package com.railway.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.railway.bean.Contact;
import com.railway.service.UserService;
import com.railway.service.impl.UserServiceImpl;
import com.railway.util.JSONUtil;

/**
 * 预定
 * Servlet implementation class reserve
 */
@WebServlet("/reserve.do")
public class Reserve extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		int train_id = 0;
		
		String start_station = null;
		
		long starting_time = 0;
		
		long terminal_time = 0;
		
		String train_name = null;
		
		double first_seatPrivce = 0;
		
		double second_seatPrivce = 0;
		
		double vip_seatPrivc = 0;
		
		int user_id = 0;
		
		if(request.getParameter("train_id") != null && "".equals(request.getParameter("train_id"))) {
			train_id = Integer.parseInt(request.getParameter("train_id"));
		}
		
		if(request.getParameter("start_station") != null && "".equals(request.getParameter("start_station"))) {
			start_station = request.getParameter("start_station");
		}
		
		if(request.getParameter("starting_time") != null && "".equals(request.getParameter("starting_time"))) {
			starting_time = Long.parseLong(request.getParameter("start_station"));
		}
		
		if(request.getParameter("terminal_time") != null && "".equals(request.getParameter("terminal_time"))) {
			terminal_time = Long.parseLong(request.getParameter("terminal_time"));
		}
		
		if(request.getParameter("train_name") != null && "".equals(request.getParameter("train_name"))) {
			train_name = request.getParameter("terminal_time");
		}
		
		if(request.getParameter("first_seatPrivce") != null && "".equals(request.getParameter("first_seatPrivce"))) {
			first_seatPrivce = Double.parseDouble( request.getParameter("first_seatPrivce"));
		}
		
		if(request.getParameter("second_seatPrivce") != null && "".equals(request.getParameter("second_seatPrivce"))) {
			second_seatPrivce = Double.parseDouble( request.getParameter("second_seatPrivce"));
		}
		
		if(request.getParameter("vip_seatPrivc") != null && "".equals(request.getParameter("vip_seatPrivc"))) {
			vip_seatPrivc = Double.parseDouble( request.getParameter("vip_seatPrivc"));
		}
		
		if(request.getParameter("user_id") != null && "".equals(request.getParameter("user_id"))) {
			user_id = Integer.parseInt( request.getParameter("user_id"));
		}
		
		UserService us = new UserServiceImpl();
		List<Contact> li = us.selectContact(user_id);
		
		List<Object> lio = new ArrayList();
		Collections.addAll(lio,train_id, start_station,starting_time,terminal_time,train_name,
				first_seatPrivce,second_seatPrivce,vip_seatPrivc,user_id,li);
		
		JSONUtil.responJSOn(response, lio);
	}

}
